<?php
/**
 * Plantilla 
 */
?>

<?php include_once 'application/libraries/ctype/views/types/includes/categories_utils.php';?>

<?php if(isset($static['javascript'])):?>
	<?php foreach($static['javascript'] as $js):?>
		<script type="text/javascript" src="<?= static_url("javascript/" .$js);?>"></script>
	<?php endforeach;?>
<?php endif;?>

<?php if(isset($static['style'])):?>
	<?php foreach($static['style'] as $css):?>
		<link rel="stylesheet" href="<?= static_url("css/" .$css);?>" type="text/css">
	<?php endforeach;?>
<?php endif;?>

<script type="text/javascript">
	var staticUrl = "<?= static_url();?>";
	var baseUrl = "<?= base_url();?>";
	var form = {
		ready: 0		
	}

	$(document).ready(function(){
		$("form button[name=saveexit]").click(function(){
			$("form input[name=action]").val("saveexit");
			return false;
		})
		$("form button[name=publish]").click(function(){
			$("form input[name=action]").val("saveexit");
			$("form select[name=ctype_status]").val("publish");
			return false;
		})
		$("form button[name=save], form button[name=saveexit], form button[name=publish]").click(function(){
			setTimeout(msgEspera, 1000);
			checkSubmitForm();
			return false;
		})
		
		$("form button[name=cancel]").click(function(){
			window.location.href="<?= $referrer; ?>";
			//window.location.href="<?= site_url("admin/nodes"); ?>";
		})
		
		<?php if($id == NULL):?>
		$("input[name=ctype_title]").keyup(function(){
			$("input[name=ctype_url_name]").val(renameUrl($(this).val()));
		})
		<?php endif;?>
	});

	function checkSubmitForm(){
		if(form.ready == 0){
			$("form[name=ctype]").submit();
		} else{			
			setTimeout(checkSubmitForm, 2000);
		}
	}

	function msgEspera(){
		var text = "Espere";
		
		if($("#espera").html() == text + "..." || $("#espera").html() == ""){
			$("#espera").html(text);
		} else{
			$("#espera").html($("#espera").html() + ".");
		}

		setTimeout(msgEspera, 1000);
	}
</script>


<div class="content-box-header">
		
		<h3>Tratando contenido <?php echo $properties['title']; ?></h3>
		
		<ul class="content-box-tabs">
			<li><a class="default-tab" href="#form-form">Formulario</a></li> <!-- href must be unique and match the id of target div -->
			<li><a href="#form-dates">Fechas</a></li>
			
			<?php if(!isset($properties['categories']) || $properties['categories']):?>
			<li><a href="#form-categories">Categorias</a></li>
			<?php endif;?>
			<li><a href="#form-metas">Metas</a></li>
			<li><a href="#form-options">Opciones</a></li>
			
		</ul>
				
		<div class="clear"></div>
		
	</div> <!-- End .content-box-header -->
	
	<div class="content-box-content">



<form name="ctype" method="post" enctype="multipart/form-data" class="ctype-form">
<?= form_hidden("ctype", "true");?>
<?= form_hidden("action", "save");?>
<?php if($referrer != NULL):?>
<?= form_hidden("referrer", $referrer);?>
<?php endif;?>



<fieldset>

<div id="form-dates" class="tab-content" style="display: none">

	<div class="nodes-calendar align-left">
		<label>Fecha de publicación</label>
		<br/><br/>

		<input type="text" id="ctype_date_publish" name="ctype_date_publish" class="small-input" value="<?php echo set_value('ctype_date_publish', $node!=NULL?($node->publish!=NULL?date("d/m/Y", strtotime($node->publish)):date('d/m/Y')):date('d/m/Y')); ?>"/>
		<span>-</span>
		<input style="width: 50px !important" type="text" id="ctype_date_publish_time" name="ctype_date_publish_time" class="small-input" value="<?php echo set_value('ctype_date_publish_time', $node!=NULL?($node->publish!=NULL?date("H:i", strtotime($node->publish)):date('H:i')):date('H:i')); ?>"/>
		<img id="ctype_date_publish_time_img" src="<?= static_url("images/icons/clock.png"); ?>" />
		<br/><br/>
		<a href="#" id="date-publish-today">Ahora</a>
	</div>
	<div class="nodes-calendar align-right">
		<label>Fecha de expiración</label>
		<br/><br/>
		
		<input type="text" id="ctype_date_expire" name="ctype_date_expire" class="small-input" value="<?php echo set_value('ctype_date_expire', $node!=NULL?($node->expiration!=NULL?date("d/m/Y", strtotime($node->expiration)):""):""); ?>"/>
		<span>-</span>
		<input style="width: 50px !important" type="text" id="ctype_date_expire_time" name="ctype_date_expire_time" class="small-input" value="<?php echo set_value('ctype_date_expire_time', $node!=NULL?($node->expiration!=NULL?date("H:i", strtotime($node->expiration)):""):""); ?>" />
		<img id="ctype_date_expire_time_img" src="<?= static_url("images/icons/clock.png"); ?>" />
		<br/><br/>
		<a href="#" id="date-expire-today">Ahora</a> |
		<a href="#" id="date-expire-clear">Limpiar</a>
	</div>
	
	<div class="clear"></div>

<script type="text/javascript">
	$(function() {
		$("#ctype_date_publish, #ctype_date_expire").datepicker({
			showOn: 'button', 
			buttonImage: '<?= static_url("images/icons/calendar.png"); ?>', 
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy',
			changeMonth: true,
			changeYear: true,
			showButtonPanel: true
		});
		
		$("#ctype_date_publish_time_img").clockpick({
			valuefield: 'ctype_date_publish_time',
			military: true,
			starthour : 0,
			endhour : 23
		}); 
		$("#ctype_date_expire_time_img").clockpick({
			valuefield: 'ctype_date_expire_time' ,
			military: true,
			starthour : 0,
			endhour : 23
		}); 


		$("#date-publish-today").click(function(){
			var now = new Date();
			var day = now.format("dd/mm/yyyy");
			var time = now.format("HH:MM");

			$("#ctype_date_publish").val(day);
			$("#ctype_date_publish_time").val(time);

			return false;
		})
		
		$("#date-expire-today").click(function(){
			var now = new Date();
			var day = now.format("dd/mm/yyyy");
			var time = now.format("HH:MM");

			$("#ctype_date_expire").val(day);
			$("#ctype_date_expire_time").val(time);

			return false;
		})

		$("#date-expire-clear").click(function(){
			$("#ctype_date_expire").val("");
			$("#ctype_date_expire_time").val("");

			return false;
		})
			
	});
	</script>

</div>

<div id="form-categories" class="tab-content" style="display: none">
	<div class="ctype-field" id="ctypes_categories">
		
		<?php $this->ci->load->view("templates/admin-simpla/utils/categories", array("field_name" => 'ctypes_categories', "field_title" => "Categorias", "categories" => $categories, "selected_categories" => isset($selected_categories)?$selected_categories:array())); ?>
	</div>
	
	<div>
		<a href="#" class="toggle" rel="add_category">+ Crear categoria</a>
	</div>
	
	<div id="add_category" style="margin-top: 10px; display: none">
		(PENDIENTE)<br/>
		<input type="text" class="small" id="" value="" name=""/><br/>
		
		<?php 
		function categoriasRecursivasSelect($categorias, $separador = "", $selectedCategory = NULL){
		
			if($categorias){
		
		 		foreach($categorias as $categoria){
					$selected = "";
					if(isset($selectedCategory) && $selectedCategory != NULL && $selectedCategory == $categoria->id){
						$selected = "selected='selected'";
					}
		 			?>
		 			<option value="<?php echo $categoria->id;?>" url="<?php echo $categoria->url_name;?>" <?php echo $selected;?>><?php echo $separador . $categoria->name;?></option> 
		 			<?php 
		 			if(isset($categoria->categories) && count($categoria->categories)>0){
		 				categoriasRecursivas($categoria->categories, $separador . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $selectedCategory);
		 			}
		 		}
			}
		}
		?>
		
		<select class="small-input" name="parent_id">
			<option value="0">Categoría superior</option>
			<?php categoriasRecursivasSelect($categories); ?>
		</select>
		<br/><br/>
		<?= form_button("add_category_button", "Añadir Categoria", 'class="button"'); ?>
		
	</div>
</div>

<div id="form-metas" class="tab-content" style="display: none">
	POR HACER..
	<div class="ctype-field" id="ctype-field-metas">
		<?php foreach($metas as $meta):?>
		<div class="meta_field">
			<input type="text" name="meta_name[]" value="<?php echo $meta->meta_key;?>" style="width: 15% !important; float: left;"/>
			<textarea name="meta_value[]" style="width: 80% !important; float: right"><?php echo $meta->meta_value;?></textarea>
			
			<div class="clear"></div>
			
			<div style="float: right">
				<?= form_button("del_meta", "Eliminar", 'class="button"'); ?>
			</div>
			<div class="clear"></div>
		</div>
		<?php endforeach; ?>
	</div>
	
	<div style="border-top: 1px solid #eee; margin-top: 15px; padding-top: 15px;">
		<div id="add_meta_fields">
			<input type="text" name="meta_name_add" value="" style="width: 15% !important; float: left;"/>
			<textarea name="meta_value_add" style="width: 80% !important; float: right;"></textarea>
			<div class="clear"></div>
		</div>
		
		<div style="float: right">
			<?= form_button("add_meta", "Añadir meta", 'class="button"'); ?>
		</div>
		<div class="clear"></div>
	</div>
	
	<div id="meta_base" style="display: none;" class="meta_field">
		<input type="text" name="meta_name_tmp"  style="width: 15% !important; float: left;"/>
		<textarea name="meta_value_tmp" style="width: 80% !important; float: right"></textarea>
		
		<div class="clear"></div>
		
		<div style="float: right">
			<?= form_button("del_meta", "Eliminar", 'class="button"'); ?>
		</div>
		<div class="clear"></div>
	</div>
	
	<script type="text/javascript">	
		$(document).ready(function(){
			$("[name=del_meta]").live("click", function(){
				$(this).parent().parent().slideUp(function(){$(this).remove();});
			});
			
			$("[name=add_meta]").click(function(){
				$meta_key = $("#add_meta_fields input[name=meta_name_add]");
				$meta_value = $("#add_meta_fields textarea[name=meta_value_add]");
				
				$meta_html = $("#meta_base").clone();
				$meta_html.attr("id", "");

				$("#ctype-field-metas").append($meta_html);
				$meta_html.show("slow");

				$meta_html.children("input[name=meta_name_tmp]").val($meta_key.val());
				$meta_html.children("textarea[name=meta_value_tmp]").val($meta_value.val());

				$meta_key.val("");
				$meta_value.val("");
				
				
				
			})
		})
	</script>
	
	
</div>

<div id="form-options" class="tab-content" style="display: none">
	<!-- Opciones... Autor (Defecto actual), Estado (Defecto Draft), Comentarios... -->

	<div class="ctype-field">
		<?= form_label('Status ', 'ctype_status');?>
		<?= form_error('ctype_status'); ?>
		<?php 
		$status_options = array(
			'draft'		=> "Borrador",
			'publish'	=> "Publicado"
		);
		$status_value = set_value("ctype_status", (($node != NULL)?$node->status:"draft"))
		?>
		<?= form_dropdown('ctype_status', $status_options, $status_value); ?>
	</div>
	
</div>



<div id="form-form" class="default-tab tab-content">
	<div class="ctype-field">
		<?php $conf_label = array(); ?>
		<?php if(form_error('ctype_title')): ?>
			<?php $conf_label['class'] = "input_error"; ?>
		<?php endif; ?>
		
		<?= form_label('Titulo *', 'ctype_title', $conf_label);?>		
		<?= form_input("ctype_title", set_value("ctype_title", (($node != NULL)?$node->title:"")), "id='ctype_title' class='big'");?>	
	</div>
	
	<div class="ctype-field">
		<?php $conf_label = array(); ?>
		<?php if(form_error('ctype_url_name')): ?>
			<?php $conf_label['class'] = "input_error"; ?>
		<?php endif; ?>
		
		<?= form_label('Url *', 'ctype_url_name', $conf_label);?>
		<?= form_input("ctype_url_name", set_value("ctype_url_name", (($node != NULL)?$node->url_name:"")), "id='ctype_url_name' class='big'");?>
	</div>
	
	<?= $fields; ?>

</div>
	
	<div class="ctype-buttons">
		<div style="float: left">
			<?= form_button("cancel", "Cancelar", 'class="button"'); ?>
		</div>
		<div style="float: right">
			<?= form_button("save", "Guardar", 'class="button"'); ?>
			<?= form_button("saveexit", "Guardar y Salir", 'class="button"'); ?>
			
			<?php if($node == NULL || $node->status != "publish"): ?>
				<?= form_button("publish", "Publicar", 'class="button"'); ?>
			<?php endif; ?>
		</div>
		
		<div style="clear: both"></div>
		
	</div>
	
	<div id="espera"></div>
	
</fieldset>
</form>

</div> <!-- End #tab2 -->  